<!-- 无集群 -->
<#if response.notOK>
<div class="main">
	<form class="form-inline" role="form">
		<div class="form-group">
			<label for="mqClusterSelect"> 集群: 暂无数据 </label>
		</div>
		<div class="form-group">
			<button type="button" class="btn btn-success" data-target="#addClusterModal" data-toggle="modal"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span>&nbsp;集群记录</button>
		</div>
	</form>
	<hr/>
</div>
<#else>
<!-- 有集群 -->
<div class="main">
	<form class="form-inline" role="form">
		<div class="form-group">
			<label for="mqClusterSelect"> 集群: </label>
			<select name="type" id="mqClusterSelect" class="form-control select2_category">
				<#list clusters as cluster>
					<option value="${cluster.id}"
						<#if cluster.id == selectedCluster.id>selected="selected"</#if>
					>${cluster.name}</option>
				</#list>
			</select>
		</div>
		<div class="form-group">
			<button type="button" class="btn btn-success" data-target="#addClusterModal" data-toggle="modal"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span>&nbsp;集群记录</button>
		</div>
		<div class="form-group">
			<button type="button" class="btn btn-success" onclick="addNameSrv()" data-target="#addNameSrvModal" data-toggle="modal"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span>&nbsp;NameServer</button>
		</div>
		<div class="form-group" style="float:right;">
			<button type="button" class="btn btn-success" data-placement="bottom" title="该功能用于已有NameServer集群关联到MQCloud中" data-target="#associateNameSrvModal" data-toggle="modal">关联NameServer</button>
		</div>
	</form>
	<hr/>
	<div style="vertical-align: middle;margin-bottom:10px;"><b>NameServer列表:</b></div>
	<table class="table table-striped table-hover" style="margin-top: 0px">
		<thead>
			<tr>
				<td>序号</td>
				<td>addr</td>
				<td>创建时间</td>
				<td>操作</td>
			</tr>
		</thead>
		<tbody>
			<#if response.empty>
				<tr class="text-center"><td colspan=4>暂无数据</td></tr>
			<#else>
				<#list response.result as ns>
					<tr>
						<td>${ns_index + 1}</td>
						<td>${ns.addr}</td>
						<td>${ns.createTime?string("yyyy-MM-dd HH:mm:ss")}</td>
						<td><button type="button" class="btn btn-sm btn-success" data-target="#nameServerOfflineModal" data-toggle="modal" onclick="offlineNameServer('${ns.addr}');">下线</button></td>
					</tr>
				</#list>
			</#if>
		</tbody>
	</table>
</div>

<!-- 新建namesrv -->
<div id="addNameSrvModal" class="modal fade" tabindex="-1">
	<div class="modal-dialog" style="width:1000px;">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
				<h4 class="modal-title">新建NameServer</h4><b style="font-size:8px;"><i>请建好/opt/mqcloud目录并将其赋权给${username}用户：chown ${username}:${username} /opt/mqcloud</i></b>
			</div>
			<div class="modal-body">
				<div class="row bs-wizard" style="border-bottom:0;">
	                <div id="nsServer" class="col-xs-1 bs-wizard-step disabled">
	                  <div class="text-center bs-wizard-stepnum">服务器选择</div>
	                  <div class="progress"><div class="progress-bar"></div></div>
	                  <a href="#" class="bs-wizard-dot"></a>
	                  <div class="bs-wizard-info text-center">安装到哪</div>
	                </div>
	                
	                <div id="nsJdk" class="col-xs-2 bs-wizard-step disabled">
	                  <div class="text-center bs-wizard-stepnum">JDK校验</div>
	                  <div class="progress"><div class="progress-bar"></div></div>
	                  <a href="#" class="bs-wizard-dot"></a>
	                  <div class="bs-wizard-info text-center">校验jdk是否配置完成，版本要求>=1.8</div>
	                </div>
	                
	                <div id="nsPort" class="col-xs-1 bs-wizard-step disabled">
	                  <div class="text-center bs-wizard-stepnum">端口校验</div>
	                  <div class="progress"><div class="progress-bar"></div></div>
	                  <a href="#" class="bs-wizard-dot"></a>
	                  <div class="bs-wizard-info text-center">检查端口是否被占用</div>
	                </div>
	                
	                <div id="nsDir" class="col-xs-1 bs-wizard-step disabled">
	                  <div class="text-center bs-wizard-stepnum">目录校验</div>
	                  <div class="progress"><div class="progress-bar"></div></div>
	                  <a href="#" class="bs-wizard-dot"></a>
	                  <div class="bs-wizard-info text-center">检查安装目录是否被占用</div>
	                </div>
	                
	                <div id="nsWget" class="col-xs-2 bs-wizard-step disabled">
	                  <div class="text-center bs-wizard-stepnum">下载安装包</div>
	                  <div class="progress"><div class="progress-bar"></div></div>
	                  <a href="#" class="bs-wizard-dot"></a>
	                  <div class="bs-wizard-info text-center">从仓库下载安装包到服务器/tmp下</div>
	                </div>
	                <div id="nsUnzip" class="col-xs-1 bs-wizard-step disabled">
	                  <div class="text-center bs-wizard-stepnum">解压安装包</div>
	                  <div class="progress"><div class="progress-bar"></div></div>
	                  <a href="#" class="bs-wizard-dot"></a>
	                  <div class="bs-wizard-info text-center">解压到安装目录下</div>
	                </div>
	                <div id="nsConfig" class="col-xs-2 bs-wizard-step disabled">
	                  <div class="text-center bs-wizard-stepnum">生成配置文件</div>
	                  <div class="progress"><div class="progress-bar"></div></div>
	                  <a href="#" class="bs-wizard-dot"></a>
	                  <div class="bs-wizard-info text-center">生成mq.conf和run.sh</div>
	                </div>
	                <div id="nsStartup" class="col-xs-1 bs-wizard-step disabled">
	                  <div class="text-center bs-wizard-stepnum">启动</div>
	                  <div class="progress"><div class="progress-bar"></div></div>
	                  <a href="#" class="bs-wizard-dot"></a>
	                  <div class="bs-wizard-info text-center">启动NameServer</div>
	                </div>
	                <div id="nsProgram" class="col-xs-1 bs-wizard-step disabled">
	                  <div class="text-center bs-wizard-stepnum">完成</div>
	                  <div class="progress"><div class="progress-bar"></div></div>
	                  <a href="#" class="bs-wizard-dot"></a>
	                  <div class="bs-wizard-info text-center">获取启动后的进程信息</div>
	                </div>
	            </div>
				<form class="form-horizontal form-bordered form-row-stripped" id="ns">
					<div class="form-body">
						<div class="form-group">
							<label class="control-label col-md-3"> 服务器: </label>
					        <div class="col-md-8">
								<select id="nsServerSelect" class="selectpicker" title="请选择" data-live-search-placeholder="搜索" name="ip" data-live-search="true"></select>
							</div>
						</div>
						<div class="form-group">
							<label class="control-label col-md-3"> 监听端口: </label>
					        <div class="col-md-8">
								<input type="text" id="nsListenPort" name="port" placeholder="默认监听端口为9876" value="9876" class="form-control" />
							</div>
						</div>
						<div class="form-group">
							<label class="control-label col-md-3"> 安装路径: </label>
					        <div class="col-md-8">
								<input type="text" value="/opt/mqcloud/ns" class="form-control" readonly/>
								<input type="hidden" name="dir" value="ns"/>
							</div>
						</div>
						<div class="form-group">
							<label class="control-label col-md-3"> 进程信息: </label>
					        <div class="col-md-8">
								<textarea id="nsProgramInfo" type="text" rows="1" class="form-control" readonly></textarea>
							</div>
						</div>
						<div class="form-group">
							<label class="control-label col-md-3"> 提示: </label>
					        <div class="col-md-8">
								<div class="form-control-static">如果安装遇到错误警告，请登录服务器解决后再<b>继续</b>执行</div>
							</div>
						</div>
					</div>
					<input type="hidden" name="cid" value="${selectedCluster.id}">
				</form>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-primary" id="installBtn" onclick="install('ns')"><span id="nsInstall">一键安装<span></button>
			</div>
		</div>
	</div>
</div>

<!-- 下线 -->
<div id="nameServerOfflineModal" class="modal fade" tabindex="-1" data-width="400">
	<div class="modal-dialog" style="width:1000px">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
				<h4 class="modal-title">确定要下线NameServer?</h4>
			</div>
			<div class="modal-body">
				<form class="form-horizontal form-bordered form-row-stripped" id="nameServerOfflineForm">
					<div class="form-body">
						<div class="form-group">
							<label class="control-label col-md-1"> 地址: </label>
					        <div class="col-md-4">
								<input type="text" name="addr" id="nameServerAddr" class="form-control" readonly />
							</div>
						</div>
						<div class="form-group">
							<label class="control-label col-md-1"> 进程: </label>
					        <div class="col-md-10">
								<textarea id="nameServerProgram" type="text" rows="10" class="form-control" readonly></textarea>
							</div>
						</div>
					</div>
					<input type="hidden" name="cid" value="${selectedCluster.id}">
				</form>
			</div>
			<div class="modal-footer">
				<button type="button" data-dismiss="modal" class="btn" >取消</button>
				<button type="button" class="btn btn-primary" onclick="nameServerOffline()" id="nameServerOfflineBtn">确定</button>
			</div>
		</div>
	</div>
</div>

<!-- 关联nameserver -->
<div id="associateNameSrvModal" class="modal fade" tabindex="-1" data-width="400">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
				<h4 class="modal-title">关联已有的NameServer</h4>
			</div>
			<div class="modal-body">
				<form class="form-horizontal form-bordered form-row-stripped" id="associateNameSrvForm">
					<div class="form-body">
						<div class="form-group">
							<label class="control-label col-md-4"> 地址: </label>
					        <div class="col-md-4">
								<input type="text" name="addr" class="form-control" placeholder="ip:port" />
							</div>
						</div>
					</div>
					<input type="hidden" name="cid" value="${selectedCluster.id}">
				</form>
			</div>
			<div class="modal-footer">
				<button type="button" data-dismiss="modal" class="btn" >取消</button>
				<button type="button" class="btn btn-primary" onclick="associateNameSrv()" id="associateNameSrvBtn">确定</button>
			</div>
		</div>
	</div>
</div>

<script>
var installId = "";
var method = "";
/**
 * 安装NameSrv
 */
function install(id){
	installId = id;
	if(!$('#'+id+'ServerSelect').val()){
		alert("请先选择服务器");
		return;
	}
	if($('#'+id+'Install').html() == "继续"){
		setTimeout(method+'()', 500);
	} else {
		validateJDK();
	}
	disableStep("installBtn");
}
/**
 * 继续
 */
function goOn(m){
	$("#"+installId+"Install").html("继续");
	active("installBtn");
	method = m;
}
/**
 * 校验jdk
 */
function validateJDK(){
	var wizardId = installId+"Jdk";
	active(wizardId);
	$.get('${request.contextPath}/admin/deploy/check/jdk',
		$("#"+installId).serialize(),
        function(data){
            if(data.status == 200){
            	var version = data.result.substring(0, 3);
            	if(version >= 1.8){
            		complete(wizardId);
            		// 校验端口
            		setTimeout('checkPort()', 1000);
            	} else {
            		alert("jdk环境校验失败，最低为1.8，目前版本为："+data.result);
            		warn(wizardId);
            		goOn("validateJDK");
            	}
		    }else{
		    	toastr.error("数据获取失败！"+data.message);  
		    	warn(wizardId);
		    	goOn("validateJDK");
		    }
       }, 'json');
}
/**
 * 端口校验
 */
function checkPort(){
	if(!$('#nsListenPort').val()){
		alert("请输入监听端口");
		return;
	}
	var wizardId = installId+"Port";
	active(wizardId);
	$.post('${request.contextPath}/admin/deploy/check/port',
		$("#"+installId).serialize(),
        function(data){
            if(data.status == 200){
            	complete(wizardId);
            	// 校验目录
            	setTimeout('checkDir()', 1000);
		    } else if(data.status == 500){
		    	toastr.error("端口被该程序占用："+data.result);  
		    	warn(wizardId);
		    	goOn("checkPort");
		    } else {
		    	toastr.error("校验失败！"+data.message);  
		    	warn(wizardId);
		    	goOn("checkPort");
		    }
       }, 'json');
}
/**
 * 目录校验
 */
function checkDir(){
	var wizardId = installId+"Dir";
	active(wizardId);
	$.post('${request.contextPath}/admin/deploy/check/dir',
		$("#"+installId).serialize(),
        function(data){
            if(data.status == 200){
            	complete(wizardId);
            	// 下载文件
        		setTimeout('wget()', 1000);
		    } else {
		    	toastr.error("校验失败！"+data.message);  
		    	warn(wizardId);
		    	goOn("checkDir");
		    }
       }, 'json');
}
/**
 * wget
 */
function wget(){
	var wizardId = installId+"Wget";
	active(wizardId);
	$.post('${request.contextPath}/admin/deploy/wget',
		$("#"+installId).serialize(),
        function(data){
            if(data.status == 200){
            	complete(wizardId);
            	// 解压文件
        		setTimeout('unzip()', 1000);
		    } else {
		    	toastr.error("下载失败！"+data.message);  
		    	warn(wizardId);
		    	goOn("wget");
		    }
       }, 'json');
}
/**
 * 解压
 */
function unzip(){
	var wizardId = installId+"Unzip";
	active(wizardId);
	$.post('${request.contextPath}/admin/deploy/unzip',
		$("#"+installId).serialize(),
        function(data){
            if(data.status == 200){
            	complete(wizardId);
            	// 配置
        		setTimeout('config()', 1000);
		    } else {
		    	toastr.error("解压失败！"+data.message);  
		    	warn(wizardId);
		    	goOn("unzip");
		    }
       }, 'json');
}
/**
 * 配置
 */
function config(){
	var wizardId = installId+"Config";
	active(wizardId);
	$.post('${request.contextPath}/admin/deploy/config/' + installId,
		$("#"+installId).serialize(),
        function(data){
            if(data.status == 200){
            	complete(wizardId);
	        	setTimeout('startup()', 1000);
		    } else {
		    	toastr.error("配置失败！"+data.message);  
		    	warn(wizardId);
		    	goOn("config");
		    }
       }, 'json');
}
/**
 * 启动实例
 */
function startup(){
	var wizardId = installId+"Startup";
	active(wizardId);
	$.post('${request.contextPath}/admin/nameserver/startup',
		$("#"+installId).serialize(),
        function(data){
            if(data.status == 200){
            	complete(wizardId);
            	var time = 5000;
            	// 检测进程
        		setTimeout('checkProgram()', time);
		    } else {
		    	toastr.error("启动失败！"+data.message);  
		    	warn(wizardId);
		    	goOn("startup");
		    }
       }, 'json');
}
/**
 * 检测启动的进程
 */
function checkProgram(){
	var wizardId = installId+"Program";
	active(wizardId);
	$.post('${request.contextPath}/admin/deploy/check/program',
		$("#"+installId).serialize(),
        function(data){
            if(data.status == 200){
            	$("#"+installId+"ProgramInfo").attr("rows", 5).html(data.result);
            	complete(wizardId);
            	alert("恭喜你，安装完成啦！");
            	$("#"+installId+"Install").parent().attr("disabled", "disabled");
		    } else {
		    	toastr.error("检查失败！"+data.message);  
		    	warn(wizardId);
		    	goOn("checkProgram");
		    }
       }, 'json');
}
function addNameSrv(){
	initServerList("nsServerSelect");
}
/**
 * 初始化server下拉列表
 */
function initServerList(componentId){
	$.get('${request.contextPath}/admin/server/all',
	        function(data){
	            if(data.status == 200){
	            	var content = "";
	            	for(var i in data.result){
	            		var server = data.result[i];
	            		content += "<option value='"+server.ip+"'>"+server.ip+"</option>";
	            	}
	        		$("#"+componentId).html(content);
	        		$("#"+componentId).selectpicker('refresh');
			    }else{
			    	toastr.error("数据获取失败！"+data.message);  
			    }
        }, 'json');
}
/**
 * 获取进程信息
 */
function program(addr, programId){
	$.get('${request.contextPath}/admin/deploy/program',
			{
				addr: addr
			},
	        function(data){
				$("#"+programId).html(data.result);
        }, 'json');
}

function offlineNameServer(addr){
	program(addr, "nameServerProgram");
	$("#nameServerAddr").val(addr);
}
function nameServerOffline(){
	disable("nameServerOfflineBtn");
	$.post('${request.contextPath}/admin/nameserver/offline',
			$("#nameServerOfflineForm").serialize(),
			function(data){
				if(data.status == 200){
		        	toastr.success("下线成功, 系统将自动刷新");
		        	reload(3000);
			    }else{
			    	toastr.error("下线失败！"+data.message);
			    	enable("nameServerOfflineBtn");
			    }
			}, 'json');
}
$('#mqClusterSelect').on('change',function(){
	window.location.href = "${request.contextPath}/admin/nameserver/list?cid="+$(this).val();
});

$(function(){
	// ns server select event
	$('#nsServerSelect').on('shown.bs.select', function(e) {
		active("nsServer");
	});
	$('#nsServerSelect').on('hidden.bs.select', function(e) {
		if(!$('#nsServerSelect').val()){
			disableStep("nsServer");
		}
	});
	$('#nsServerSelect').on('changed.bs.select', function(e) {
		complete("nsServer");
	});
});

function warn(id){
	$("#"+id).addClass("warn");
}
function disableStep(id){
	$("#"+id).removeClass("active").addClass("disabled");
}
function active(id){
	$("#"+id).removeClass("disabled").removeClass("warn").addClass("active");
}
function complete(id){
	$("#"+id).removeClass("active").removeClass("warn").addClass("complete");
}
</script>
</#if>

<div id="addClusterModal" class="modal fade" tabindex="-1" data-width="400" data-keyboard="false">
	<div class="modal-dialog">
		<div class="modal-content">

			<div class="modal-header">
				<h4 class="modal-title">新增集群记录</h4>
			</div>

			<form class="form-horizontal form-bordered form-row-stripped" id="addClusterForm">
				<div class="modal-body">
					<div class="row">
						<div class="col-md-12">
							<div class="form-body">
								<div class="form-group">
									<label class="control-label col-md-4"> 集群id: </label>
									<div class="col-md-5">
										<input type="text" name="id" placeholder="集群id" data-toggle="modal" title="集群id将作为name server的一部分，使用递增的数字，自己保证唯一。" class="form-control" />
									</div>
								</div>
								<div class="form-group">
									<label class="control-label col-md-4"> 集群名: </label>
									<div class="col-md-5">
										<input type="text" name="name" placeholder="即brokerClusterName的值" class="form-control" />
									</div>
								</div>
								<div class="form-group">
									<label class="control-label col-md-4"> 虚拟通道: </label>
									<div class="col-md-5 checkbox">
									    <input type="radio" name="vipChannelEnabled" value="1" checked> 
									    	<span data-toggle="modal" title="rocketmq4.x设置为开启">开启</span>
									    <input type="radio" name="vipChannelEnabled" value="0">
									    	<span data-toggle="modal" title="rocketmq3.x需要关闭">关闭</span>
									</div>
								</div>
								<div class="form-group">
									<label class="control-label col-md-4"> 环境: </label>
									<div class="col-md-5 checkbox">
									    <input type="radio" name="online" value="1" checked> 
									    	<span data-toggle="modal" title="线上集群，会开启流量抓取和监控">线上</span>
									    <input type="radio" name="online" value="0">
									    	<span data-toggle="modal" title="测试集群，不会进行流量抓取和监控">测试</span>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
				<div class="modal-footer">
					<button type="button" data-dismiss="modal" class="btn" >取消</button>
					<button type="button" class="btn btn-primary" id="addClusterBtn" onclick="addCluster()">创建</button>
				</div>
			</form>
		</div>
	</div>
</div>
<script>
$("[data-toggle='modal']").tooltip();
function addCluster(){
	disable("addClusterBtn");
	$.post('${request.contextPath}/admin/cluster/add',
		$("#addClusterForm").serialize(),
        function(data){
            if(data.status == 200){
				toastr.success("集群创建成功，将自动刷新");  
				reload(3000);
		    }else{
		    	toastr.error("集群创建失败！"+data.message);
		    	enable("addClusterBtn");
		    }
        }, 'json');
}

function associateNameSrv(){
	disable("associateNameSrvBtn");
	$.post('${request.contextPath}/admin/nameserver/add',
		$("#associateNameSrvForm").serialize(),
        function(data){
            if(data.status == 200){
				toastr.success("关联成功，将自动刷新");  
				reload(3000);
		    }else{
		    	toastr.error("关联失败！"+data.message);
		    	enable("associateNameSrvBtn");
		    }
        }, 'json');
}
</script>